package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.Type;
import com.bwie.service.TypeService;
import com.bwie.mapper.TypeMapper;
import com.bwie.utils.Result;
import com.bwie.vo.TypeVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @author 陈彤彤
* @description 针对表【t_type(类型表)】的数据库操作Service实现
* @createDate 2025-07-21 16:55:43
*/
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type>
    implements TypeService{

    @Autowired
    TypeMapper typeMapper;

    @Autowired
    FastFileStorageClient fileStorageClient;

   /**
   * @param typeVo:
   * @return Result
   * @author :陈彤彤
   * @description typeVo
   * @date 2025/7/21 17:28
   */
    @Override
    public Result typeTreeList(TypeVo typeVo) {
        /*多条件查询*/
        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        if (typeVo.getKeyWord() != null && typeVo.getKeyWord() !=""){
            wrapper.like(Type::getTypeName,typeVo.getKeyWord());
        }
        if (typeVo.getTypeStatus() != null){
            wrapper.eq(Type::getTypeStatus,typeVo.getTypeStatus());
        }
        if (typeVo.getKeyWord() != null && typeVo.getKeyWord() != "" || typeVo.getTypeStatus() !=null){
            //查询所有分类
            List<Type> typeList = typeMapper.selectList(wrapper);
            return Result.ok().setData(typeList);
        }

        //查询所有分类
        List<Type> typeList = typeMapper.selectList(null);

        /*构造类型树*/
        List<Tree<String>> treeList = TreeUtil.build(typeList, "0", (type, tree) -> {
            tree.setId(type.getTypeId() + "");
            tree.setName(type.getTypeName() + "");
            tree.setParentId(type.getTypePid() + "");

            tree.putExtra("typeImage",type.getTypeImage());
            tree.putExtra("typeStatus",type.getTypeStatus());
            tree.putExtra("typeTop",type.getTypeTop());
            tree.putExtra("typeName",type.getTypeName());
            tree.putExtra("typePid",type.getTypePid());
        });
        return Result.ok().setData(treeList);
    }

    /**
    * @param file:
    * @return Result
    * @author :陈彤彤
    * @description 分类图片上传
    * @date 2025/7/21 20:04
    */
    @Override
    public Result uploadFile(MultipartFile file) {
        try {
            /*获取文件扩展名*/
            String extName = FileNameUtil.extName(file.getOriginalFilename());

            /*文件上传*/
            StorePath path = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);

            /*获取文件上传路径*/
            String url = "http://192.168.80.192:8888/" + path.getFullPath();

            return Result.ok().setData(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error().setMsg("上传失败");
        }
    }

    /**
    * @param type:
    * @return Result
    * @author :陈彤彤
    * @description 添加分类树信息
    * @date 2025/7/21 20:08
    */
    @Override
    public Result addTypeTree(Type type) {
        /*校验分类名称是否存在*/
        Type selectOne = typeMapper.selectOne(new QueryWrapper<Type>().lambda().eq(Type::getTypeName, type.getTypeName()));
        if (selectOne != null){
            return Result.error().setMsg("分类名称已存在");
        }

        /*添加分类树信息*/
        type.setCreateBy("admin");
        type.setCreateTime(new Date());
        type.setUpdateBy("admin");
        type.setUpdateTime(new Date());
        typeMapper.insert(type);
        return Result.ok();
    }
}




